package org.hgvc.webapp.ets.jsf.beans;

import java.util.List;

import javax.faces.component.UIParameter;
import javax.faces.event.ActionEvent;

import org.hgvc.webapp.ets.bo.ActionBulletin;
import org.hgvc.webapp.ets.constant.ActionBulletinConstant;
import org.hgvc.webapp.ets.spring.service.SystemService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.bo.User;
import org.jocsb.base.exception.BaseException;
import org.jocsb.base.util.BeanUtil;


public class ActionBulletinBean extends AbstractBean{

	public static final String BEAN_NAME="actionBulletinBean";
	private List<ActionBulletin> actionBulletinList;
	private ActionBulletin actionBulletin;
	private SystemService systemService;
	public static final String NAV_HOME="home";
	public String ackType=ActionBulletinConstant.AB_ACK_TYPE;
	public String actionType=ActionBulletinConstant.AB_ACTION_TYPE;
	private User user;
	
	
	
	public User getUser() {
		user = getThisUser();
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public SystemService getSystemService() {
		return systemService;
	}
	public void setSystemService(SystemService systemService) {
		this.systemService = systemService;
	}
	public List<ActionBulletin> getActionBulletinList() {
		try{
			actionBulletinList = systemService.getActionBulletinList(getThisUser().getUserId());
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return actionBulletinList;
	}
	public void setActionBulletinList(List<ActionBulletin> actionBulletinList) {
		this.actionBulletinList = actionBulletinList;
	}

	public ActionBulletin getActionBulletin() {
		return actionBulletin;
	}
	public void setActionBulletin(ActionBulletin actionBulletin) {
		this.actionBulletin = actionBulletin;
	}
	public String getAckType() {
		return ackType;
	}
	public String getActionType() {
		return actionType;
	}
	public void actionListener(ActionEvent actionEvent){
		UIParameter param = (UIParameter)actionEvent.getComponent().findComponent("abId");
		String abId = (String)param.getValue();
		for(ActionBulletin ab:actionBulletinList){
			if(ab.getActionBulletinId().equalsIgnoreCase(abId)){
				actionBulletin = ab;
				break;
			}
		}
	}
	
	public String action(){
		String[] param = {actionBulletin.getActionNav(),actionBulletin.getActionParam1()
				,actionBulletin.getActionParam2(),actionBulletin.getActionParam3()};
		String beanMethod = actionBulletin.getActionBean()+"."+actionBulletin.getActionMethod();
		return BeanUtil.invokeAction(beanMethod, param,actionBulletin.getActionNo());
	}
	
	public void ackListener(ActionEvent actionEvent){
		UIParameter param = (UIParameter)actionEvent.getComponent().findComponent("abId");
		String abId = (String)param.getValue();
		for(ActionBulletin ab:actionBulletinList){
			if(ab.getActionBulletinId().equalsIgnoreCase(abId)){
				actionBulletin = ab;
				break;
			}
		}
	}
	
	public String ackAction(){
		try{
			systemService.ackActionBulletin(actionBulletin, getThisUser());
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return null;
	}
	
}
